package com.mmy.controller.visa;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.visa.TVisaCollection;
import com.mmy.domain.visa.TVisaOrder;
import com.mmy.domain.visa.TVisaStuff;
import com.mmy.domain.visa.VVisaBase;
import com.mmy.service.impl.visa.TVisaBaseServiceImpl;
import com.mmy.service.impl.visa.TVisaCollectionServiceImpl;
import com.mmy.service.impl.visa.TVisaOrderServiceImpl;
import com.mmy.service.impl.visa.TVisaStuffServiceImpl;
import com.mmy.service.impl.visa.VVisaBaseServiceImpl;

/**
 * 类的介绍：签证订单
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class TVisaOrderController {

	@Autowired
	TVisaOrderServiceImpl tVisaOrderServiceImpl;
	@Autowired
	TVisaBaseServiceImpl tVisaBaseServiceImpl;
	@Autowired
	VVisaBaseServiceImpl vVisaBaseServiceImpl;
	@Autowired
	TVisaStuffServiceImpl tVisaStuffServiceImpl;
	@Autowired
	TVisaCollectionServiceImpl tVisaCollectionServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 新建签证预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterreservevisaindex")
	public String EnterReserveVisa(HttpServletRequest request, Model model) {

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));

		VVisaBase vVisaBase = vVisaBaseServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("vVisaBase", vVisaBase);

		model.addAttribute("iVisaID", iVisaID);

		return "webbackground/visamanagement/reservevisaindex";

	}
	/**
	 * 函数功能说明： 提交--新建签证
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 liys
	 * @date 创建日期
	 */

	@RequestMapping(value = "/subreservevisaindex")
	public String ReserveVisa(TVisaOrder tVisaOrder, HttpSession session,HttpServletRequest request) {
		// 获取最大ID
		tVisaOrder.setID(common.createMaxID(UtilConstants.TableName.T_VISA_ORDER));

		// 获得订单编号
		String strVisaID = request.getParameter("VisaID");
		int iVisaID = Integer.parseInt(strVisaID);
		String cOrderID = common.createOrderCode(UtilConstants.TableName.T_VISA_ORDER, iVisaID);
		VVisaBase vVisaBase = vVisaBaseServiceImpl.findByVisaID(iVisaID);
			tVisaOrder.setCreateDate(new Date());
			tVisaOrder.setReserveDate(new Date());
			tVisaOrder.setFloor(vVisaBase.getFloor());
			tVisaOrder.setPeer(vVisaBase.getPeer());
			tVisaOrder.setPrice(vVisaBase.getPrice());
			BigDecimal bPrice = vVisaBase.getPrice();
			tVisaOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tVisaOrder.setCreator(tSysUser.getUserName());
			tVisaOrder.setReserveUserID(tSysUser.getUserID());
			tVisaOrder.setOrderID(cOrderID);
			tVisaOrderServiceImpl.save(tVisaOrder.getID());
			tVisaOrder.setOrderState((byte) 0);
			tVisaOrder.setCashierState((byte) 0);
			tVisaOrder.setAccountState((byte) 0);
			tVisaOrder.setInvoiceState((byte) 0);
			tVisaOrder.setRefundState((byte) 0);
			tVisaOrder.setRemittanceState((byte) 0);
			tVisaOrderServiceImpl.save(tVisaOrder);
			return "redirect:visainfo?iVisaID=" + Integer.parseInt(strVisaID);
	}

	/**
	 * 函数功能说明： 预留转订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterpayordervisa")
	public String EnterPayOrderVisa(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(iID);
		model.addAttribute("tVisaOrder", tVisaOrder);
		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		model.addAttribute("iVisaID", iVisaID);
		return "webbackground/visamanagement/payordervisa";
	}

	@RequestMapping(value = "/subpayordervisa")
	public String PayOrderVisa(TVisaOrder tVisaOrder, HttpSession session, String VisaID,
			TVisaCollection tVisaCollection) throws Exception {

		try {
//			 定义一个新的实体类对象 
			TVisaOrder dtVisaOrder = tVisaOrderServiceImpl.findByID(tVisaOrder.getID());
			dtVisaOrder.setPayType(tVisaOrder.getPayType());
			dtVisaOrder.setOrderState((byte) 2);
			dtVisaOrder.setOrderDate(new Date());
			dtVisaOrder.setOrderName(tVisaOrder.getOrderName());
			dtVisaOrder.setOrderTel(tVisaOrder.getOrderTel());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtVisaOrder.setOrderUserID(tSysUser.getUserID());
			dtVisaOrder.setReceivable(tVisaOrder.getReceivable());
			dtVisaOrder.setVoucher(tVisaOrder.getVoucher());
			dtVisaOrder.setReceipt(tVisaOrder.getReceipt());
			tVisaOrderServiceImpl.save(dtVisaOrder);

			/* 同时创建材料整理表 */
			/* 通过VisaID获取材料编号 */
			List<TVisaStuff> lstStuffID = tVisaStuffServiceImpl.findByVisaID(Integer.parseInt(VisaID));
			for (int i = 0; i < lstStuffID.size(); i++) {
				int iStuffID = lstStuffID.get(i).getStuffID();
				/* 获取最大值 */
				Object CollectionID = tVisaCollectionServiceImpl.getMaxCollectionID();
				/* 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 */
				if (CollectionID == null) {
					tVisaCollection.setCollectionID(1);
				} else {
					tVisaCollection.setCollectionID(Integer.parseInt(CollectionID.toString()) + 1);
				}

				tVisaCollection.setCreateDate(new Date());
				tVisaCollection.setCreator(tSysUser.getUserName());
				tVisaCollectionServiceImpl.save(tVisaCollection.getCollectionID());
				tVisaCollection.setOrderID(dtVisaOrder.getOrderID());
				tVisaCollection.setStuffID(iStuffID);
				tVisaCollectionServiceImpl.save(tVisaCollection);
			}
			return "redirect:printvisa?iID=" + tVisaOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 取消订单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelordervisa")
	public String EnterCancelOrderVisa(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(iID);
		model.addAttribute("tVisaOrder", tVisaOrder);

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		model.addAttribute("iVisaID", iVisaID);

		return "webbackground/visamanagement/cancelordervisa";

	}

	@RequestMapping(value = "/subcancelordervisa")
	public String CancelOrderVisa(TVisaOrder tVisaOrder, HttpSession session, String VisaID) throws Exception {

		try {
//			 定义一个新的实体类对象 
			TVisaOrder dtVisaOrder = tVisaOrderServiceImpl.findByID(tVisaOrder.getID());

			dtVisaOrder.setOrderState((byte) 4);
			dtVisaOrder.setRefund(tVisaOrder.getRefund());
			tVisaOrderServiceImpl.save(dtVisaOrder);

			return "redirect:printcancelvisa?iID=" + tVisaOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 修改签证预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdatereservevisa")
	public String EnterUpdateReserveVisa(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(iID);
		model.addAttribute("tVisaOrder", tVisaOrder);
		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		model.addAttribute("iVisaID", iVisaID);

		return "webbackground/visamanagement/updatereservevisa";

	}

	@RequestMapping(value = "/updatereservevisa")
	public String UpdateReserveVisa(TVisaOrder tVisaOrder, HttpSession session, String VisaID) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TVisaOrder dtVisaOrder = tVisaOrderServiceImpl.findByID(tVisaOrder.getID());
			dtVisaOrder.setReserveName(tVisaOrder.getReserveName());
			dtVisaOrder.setReserveTel(tVisaOrder.getReserveTel());
			dtVisaOrder.setMemo(tVisaOrder.getMemo());
			dtVisaOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtVisaOrder.getPrice();

			dtVisaOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtVisaOrder.setReviser(tSysUser.getUserName());
			tVisaOrderServiceImpl.save(dtVisaOrder);
			return "redirect:visainfo?iVisaID=" + Integer.parseInt(VisaID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 取消预留
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelreservevisa")
	public String CancelReserveVisa(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(iID);

		tVisaOrder.setOrderState((byte) 3);

		tVisaOrderServiceImpl.save(tVisaOrder);

		model.addAttribute("tVisaOrder", tVisaOrder);
		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		model.addAttribute("iVisaID", iVisaID);

		VVisaBase vVisaBase = vVisaBaseServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("vVisaBase", vVisaBase);

		List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("orderlist", lstVisaOrder);

		/* 查询该ID下所需的材料 */
		List<TVisaStuff> lstVisaStuff = tVisaStuffServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("newTVisaStuff", lstVisaStuff);

		return "webbackground/visamanagement/visainfo";

	}

	/**
	 * 函数功能说明： 修改签证订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateordervisa")
	public String EnterUpdateOrderVisa(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		TVisaOrder tVisaOrder = tVisaOrderServiceImpl.findByID(iID);
		model.addAttribute("tVisaOrder", tVisaOrder);
		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		model.addAttribute("iVisaID", iVisaID);

		return "webbackground/visamanagement/updateordervisa";

	}

	@RequestMapping(value = "/updateordervisa")
	public String UpdateOrderVisa(TVisaOrder tVisaOrder, HttpSession session, String VisaID) throws Exception {
		try {
//			 定义一个新的实体类对象 
			TVisaOrder dtVisaOrder = tVisaOrderServiceImpl.findByID(tVisaOrder.getID());
			dtVisaOrder.setOrderName(tVisaOrder.getOrderName());
			dtVisaOrder.setOrderTel(tVisaOrder.getOrderTel());
			dtVisaOrder.setMemo(tVisaOrder.getMemo());
			dtVisaOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtVisaOrder.getPrice();

			dtVisaOrder.setOrderAmount(bPrice);
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtVisaOrder.setReviser(tSysUser.getUserName());
			tVisaOrderServiceImpl.save(dtVisaOrder);
			return "redirect:visainfo?iVisaID=" + Integer.parseInt(VisaID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除取消预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delreservevisaindex")
	public String DelReserveVisa(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tVisaOrderServiceImpl.delVisaOrder(lstID);

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		model.addAttribute("iVisaID", iVisaID);
		VVisaBase vVisaBase = vVisaBaseServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("vVisaBase", vVisaBase);

		List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("orderlist", lstVisaOrder);

		return "webbackground/visamanagement/visainfo";
	}

	/**
	 * 函数功能说明： 删除取消订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delordervisaindex")
	public String DelOrderVisa(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tVisaOrderServiceImpl.delVisaOrder(lstID);

		int iVisaID = Integer.parseInt(request.getParameter("iVisaID"));
		model.addAttribute("iVisaID", iVisaID);

		VVisaBase vVisaBase = vVisaBaseServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("vVisaBase", vVisaBase);

		List<TVisaOrder> lstVisaOrder = tVisaOrderServiceImpl.findByVisaID(iVisaID);
		model.addAttribute("orderlist", lstVisaOrder);

		return "webbackground/visamanagement/visainfo";
	}

	
}
